package com.bloveoa.zkkd.scheduled;

/**
 * Created by wuhaochao on 2017/7/21.
 */


import com.alibaba.fastjson.JSONObject;
import com.bloveoa.zkkd.doman.BoxInfoTokenNumModel;
import com.bloveoa.zkkd.doman.LockTokenAward;
import com.bloveoa.zkkd.doman.LockedTokenModel;
import com.bloveoa.zkkd.doman.TokenRemainingModel;
import com.bloveoa.zkkd.service.LockedTokenService;
import com.bloveoa.zkkd.service.UserService;
import com.bloveoa.zkkd.service.impl.BoxInfoServiceImpl;
import com.bloveoa.zkkd.service.impl.TokenRemainingServiceImpl;
import com.bloveoa.zkkd.theadPoolConfig.AsyncTranscationTask;
import com.bloveoa.zkkd.util.FtpUtil;
import com.bloveoa.zkkd.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

import static com.bloveoa.zkkd.util.StringUtils.getFromFtp;

/**
 * 定时任务,
 */
@Component
public class MyScheduled {
    private final AsyncTranscationTask asyncTranscationTask;
    private final BoxInfoServiceImpl boxInfoService;
    private final LockedTokenService lockedTokenService;
    private final TokenRemainingServiceImpl service;
    private final UserService userService;

    public MyScheduled(AsyncTranscationTask asyncTranscationTask, BoxInfoServiceImpl boxInfoService, LockedTokenService lockedTokenService, TokenRemainingServiceImpl service, UserService userService) {
        this.asyncTranscationTask = asyncTranscationTask;
        this.boxInfoService = boxInfoService;
        this.lockedTokenService = lockedTokenService;
        this.service = service;
        this.userService = userService;
    }

    /*每天每20秒执行一次*/
    @Scheduled(cron = "0/20 * * * * ?")
    @Transactional
    public void executeTask2() {
        //  System.out.println(">>>>>>>>>>>>>>>executeTask2<<<<<<<<<<<<<");
        try {
            asyncTranscationTask.sendToken();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    /*每天每20秒执行一次*/
  @Scheduled(cron = "0 0 2 * * ?")
  // @Scheduled(fixedRate = 10 * 1000)
    @Transactional
    public void executeTask3() {
        //  System.out.println(">>>>>>>>>>>>>>>executeTask3<<<<<<<<<<<<<");
        List<LockedTokenModel> allByFlagDelete = lockedTokenService.findAllByFlagDeleteIsFalseAndLockEndTimeBefore(new Date());
        //查询配置信息
        TokenRemainingModel tokenRemainingModel = service.queryModel();
        //获取锁仓奖励信息
        String lockedAward = tokenRemainingModel.getLockedAward();

        LockTokenAward lockTokenAward = JSONObject.parseObject(lockedAward,LockTokenAward.class);

        int awardNum = allByFlagDelete.stream().mapToInt(x -> userService.updateLockNum(lockTokenAward, x))
                .sum();
        tokenRemainingModel .setTLockedNum(tokenRemainingModel.getTLockedNum()-awardNum);
        service.saveModel(tokenRemainingModel);
    }

    /*每天每20秒执行一次*/
    @Scheduled(cron = "0 0 2 * * ?")
    @Transactional
    public void executeTask() {
       // System.out.println("ftp---start");
        try {
            //获取出来数据
            TokenRemainingModel tokenRemainingModel = service.queryModel();
            Integer tBoxNum = tokenRemainingModel.getTBoxNum();
            if (tBoxNum <= 0) {
                return;
            }
            List<String> strings = FtpUtil.readFileByLine(StringUtils.getFileName());
            if (strings == null || strings.size() == 0)
                return;
            BoxInfoTokenNumModel fromFtp = getFromFtp(strings);
            //获取平均值
            double num = fromFtp.getSumTokenNum() * 1.0 / fromFtp.getSumLong() * 1.0;
            fromFtp.getDatas().forEach(boxInfoModel -> {
                //设置每个盒子每天奖励的数量
                //查询当前用户是否绑定了用户，如果绑定了用户就把数量加到用户上面
                boxInfoModel.setTokenNun((int) (num * boxInfoModel.getTOnLong()));
            });
            //插入，如果存在就更新
            boxInfoService.updateOrInsert(fromFtp.getDatas());
            tBoxNum -= fromFtp.getSumTokenNum();
            tokenRemainingModel.setTBoxNum(tBoxNum);
            service.saveModel(tokenRemainingModel);
          // System.out.println(strings);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
